home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
59454
/
59454.xpi
/
content
/
overlay.js
< prev
next >
Wrap
Text File
|
2010-01-26
|
22KB
|
526 lines
BartUniversalSearch.UniversalSearchClass = BartUtils.defineClass(
{
name: "UniversalSearchClass",
construct: function()
{
this.bubble = new BartUniversalSearch.BubbleClass();
// name of the default engine
var defaultEngineName = BartUniversalSearch.Preference.getDefaultEngine();
var engine = BartUniversalSearch.SearchEngineWrapper.getEngine(defaultEngineName, true);
defaultEngineName = engine.getName();
this._setDefaultEngine(defaultEngineName);
},
methods:
{
_setDefaultEngine: function(engineName)
{
this.defaultEngineName = engineName;
BartUniversalSearch.Preference.setDefaultEngine(engineName);
},
_getPopupMenu: function()
{
return document.getElementById("bart-ibrowser-UniversalSearch-popup");
},
_openEngineManager: function()
{
var mediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var window = mediator.getMostRecentWindow("Browser:SearchManager");
if (window)
{
window.focus();
}
else
{
setTimeout(function ()
{
openDialog("chrome://browser/content/search/engineManager.xul", "_blank", "chrome,dialog,modal,centerscreen");
}, 0);
}
},
_buildSearchEnginePopupMenu: function(popup)
{
var engines = BartUniversalSearch.SearchEngineWrapper.getAvailableSearchEngines();
var menuItem;
for (var i = 0; i < engines.length; i++)
{
menuItem = document.createElement("menuitem");
menuItem.setAttribute("label", engines[i].getName());
menuItem.setAttribute("tooltiptext", "Search using " + engines[i].getName());
menuItem.setAttribute("class", "menuitem-iconic");
var iconUrl = engines[i].getIconUrl(BartUniversalSearch.UniversalSearchClass.ICON_SEARCHDEFAULT);
menuItem.setAttribute("image", iconUrl);
menuItem.setAttribute(BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID, BartUniversalSearch.UniversalSearchClass.CMD_ID_SEARCH);
menuItem.engine = engines[i].getName();
if(this.defaultEngineName == engines[i].getName())
{
menuItem.setAttribute("style", "font-weight:bold");
}
popup.appendChild(menuItem);
}
if(i > 0)
{
menuItem = document.createElement("menuseparator");
popup.appendChild(menuItem);
}
// create engine management menuitem
menuItem = document.createElement("menuitem");
menuItem.setAttribute("label", "Manage Search Engines...");
menuItem.setAttribute("class", "menuitem-iconic");
menuItem.setAttribute(BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID, BartUniversalSearch.UniversalSearchClass.CMD_ID_MANAGEENGINE);
popup.appendChild(menuItem);
},
_showSearchEngineMenu: function(anchor)
{
var popup = this._getPopupMenu();
if(popup)
{
// Remove old
BartDOMUtils.removeAllChildren(popup);
this._buildSearchEnginePopupMenu(popup);
this._attachEventListener(popup.getElementsByTagName("menuitem"), "click");
popup.openPopup(anchor, "after_start", 0, 0, true, true);
}
},
_init: function()
{
},
_getFocusedDocument: function()
{
return document.commandDispatcher.focusedWindow.document;
},
_createBubble: function()
{
var icons = [];
var imgProps = {};
var linkProps = {};
imgProps["alt"] = "";
imgProps["class"] = "universalsearch-bubble-icon";
//imgProps["draggable"] = "false";
imgProps["ondragstart"] = "return false;";
//linkProps["draggable"] = "false";
linkProps["ondragstart"] = "return false;";
linkProps["class"] = "universalsearch-bubble-link";
linkProps["onclick"] = "return false;";
if(BartUniversalSearch.Preference.showCopyButton())
{
imgProps[BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID] = BartUniversalSearch.UniversalSearchClass.CMD_ID_COPY;
imgProps["id"] = BartUniversalSearch.UniversalSearchClass.CMD_ID_COPY;
imgProps["src"] = BartUniversalSearch.UniversalSearchClass.ICON_COPY;
imgProps["title"] = "Copy";
icons.push(this.bubble.createIcon(imgProps, linkProps));
}
imgProps[BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID] = BartUniversalSearch.UniversalSearchClass.CMD_ID_SEARCH;
imgProps["id"] = BartUniversalSearch.UniversalSearchClass.CMD_ID_SEARCH;
var defaultEngine = BartUniversalSearch.SearchEngineWrapper.getEngine(this.defaultEngineName, true);
var engineIconUrl = defaultEngine.getIconUrl(BartUniversalSearch.UniversalSearchClass.ICON_SEARCHDEFAULT);
imgProps["src"] = engineIconUrl;
imgProps["class"] = "universalsearch-bubble-icon";
imgProps["title"] = this._getSearchButtonTitle(defaultEngine.getName());
icons.push(this.bubble.createIcon(imgProps, linkProps));
imgProps[BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID] = BartUniversalSearch.UniversalSearchClass.CMD_ID_MORESEARCHES;
imgProps["id"] = BartUniversalSearch.UniversalSearchClass.CMD_ID_MORESEARCHES;
imgProps["src"] = BartUniversalSearch.UniversalSearchClass.ICON_DOWNARROW;
imgProps["class"] = "universalsearch-bubble-dropdownicon";
imgProps["title"] = "More searches...";
linkProps["class"] = "universalsearch-bubble-dropdownlink";
icons.push(this.bubble.createIcon(imgProps, linkProps));
return this.bubble.createBubble(this._getFocusedDocument(), icons);
},
_handleBubbleEvent: function(event)
{
var element = event.target;
//if(element.tagName.toLowerCase() == ("img"))
{
var cmdId = element.getAttribute(BartUniversalSearch.UniversalSearchClass.ATTR_CMD_ID);
switch(cmdId)
{
case BartUniversalSearch.UniversalSearchClass.CMD_ID_COPY:
{
goDoCommand("cmd_copy");
}
break;
case BartUniversalSearch.UniversalSearchClass.CMD_ID_MORESEARCHES:
{
this._showSearchEngineMenu(element);
}
break;
case BartUniversalSearch.UniversalSearchClass.CMD_ID_MANAGEENGINE:
{
this._openEngineManager();
}
break;
case BartUniversalSearch.UniversalSearchClass.CMD_ID_SEARCH:
{
var engine = element.engine;
if(engine)
{
var selectedEngine = BartUniversalSearch.SearchEngineWrapper.getEngine(engine, true);
if(engine != this.defaultEngineName)
{
this._setSearchButtonAttributes(engine,
selectedEngine.getIconUrl(BartUniversalSearch.UniversalSearchClass.ICON_SEARCHDEFAULT));
// change default engine
this._setDefaultEngine(engine);
}
var selection = document.commandDispatcher.focusedWindow.getSelection();
var searchUrl = selectedEngine.getSearchUrl(selection.toString().trim());
BartUniversalSearch.UniversalSearchClass.openSearchPage(searchUrl);
}
}
break;
event.cancelBubble = true;
//event.preventDefault();
return false;
}
}
},
_attachEventListener: function(elements, eventName)
{
for(var i = 0; i < elements.length; i++)
{
elements[i].addEventListener(eventName, BartUniversalSearch.UniversalSearchClass._onBubbleEvent, false);
}
},
onMouseUp: function(event)
{
// check if it's left mouse button
if(event.button != 0)
return true;
var popup = this._getPopupMenu();
var doc = this._getFocusedDocument();
if(!doc)
return true;
var target = event.target;
do
{
if(target == this.bubble.getBubble())
return true; // current event is triggered by our bubble
if(target == popup)
return true;
if(target == doc)
break;
target = target.parentNode;
}while(target);
if(!target)
return true; // not clicked on the focused document
//get the selection
try
{
var selection = document.commandDispatcher.focusedWindow.getSelection();
var reverseSelection = false;
if(selection && selection.toString().trim() != "")
{
//alert(selection.toString());
var bubble = this._createBubble();
//get the index and range and insert our bubble
var firstRange = selection.getRangeAt(0);
var newRange = doc.createRange();
newRange.setStart(selection.focusNode, selection.focusOffset);
newRange.setEnd(selection.focusNode, selection.focusOffset);
if(firstRange.startOffset == selection.focusOffset && firstRange.startContainer == selection.focusNode)
{
// our new range is before the first range.
reverseSelection = true;
// If it's a reverse selection, popup will make selection changed. Don't know why...
// The following code is coping with this issue
if(selection.focusNode.nodeType == Node.TEXT_NODE) // is it possible that nodeType is not TEXT?
{
var tmpRange = doc.createRange();
// must backup these values of selection, because it seems that selection properties are changed during the following actions
var focusOffset = selection.focusOffset;
var anchorOffset = selection.anchorOffset;
var newTextNode = selection.focusNode.splitText(focusOffset);
if(selection.anchorNode == selection.focusNode) // selection is made in the same node
{
// keep only the selected text
if(anchorOffset - focusOffset > 0)
newTextNode.splitText(anchorOffset - focusOffset);
// remove existing ranges (it should be only one range) because we created a new one
//if(selection.rangeCount == 1)
// selection.removeAllRanges();
}
//firstRange.setStartAfter(newTextNode); // it seems this method doesn't work properly
// make sure the original selection still exists
tmpRange.selectNodeContents(newTextNode);
try
{
firstRange.setStartAfter(newTextNode);
}
catch(e)
{}
// add the new range to the selection to make it selected
selection.addRange(tmpRange);
/*if(selection.rangeCount > 1) // some range in current selection already contains content of the tmpRange, so remove the tmpRange
{
tmpRange.collapse(true);
selection.removeRange(tmpRange);
tmpRange.detach();
}*/
}
}
else
{
// new range is after the first range. It works well. Nothing to do
}
newRange.insertNode(bubble);
newRange.detach();
// offset the bubble
var offset = -(bubble.offsetHeight + 5);
bubble.setAttribute("style", "margin-top: " + offset + "px");
this._attachEventListener(bubble.getElementsByTagName("img"), "click");
// set the default search info
var defaultEngine = BartUniversalSearch.SearchEngineWrapper.getEngine(this.defaultEngineName, true);
this._setSearchButtonAttributes(defaultEngine.getName(),
defaultEngine.getIconUrl(BartUniversalSearch.UniversalSearchClass.ICON_SEARCHDEFAULT));
}
else
{
this.bubble.removeBubble();
}
}
catch(e)
{
this.bubble.removeBubble();
}
},
_setSearchButtonAttributes: function(engineName, iconUrl)
{
var searchButton = this._getFocusedDocument().getElementById(BartUniversalSearch.UniversalSearchClass.CMD_ID_SEARCH);
if(searchButton)
{
searchButton.engine = engineName;
// Change icon image and title.
searchButton.setAttribute("src", iconUrl);
searchButton.setAttribute("title", this._getSearchButtonTitle(engineName));
}
},
_getSearchButtonTitle: function(engineName)
{
return "Search using " + engineName;
}
},
statics:
{
_onBubbleEvent: function(event)
{
BartUniversalSearch.gSearch._handleBubbleEvent(event);
},
_loadCssFile: function(fileName)
{
var resourceUrl = "chrome://UniversalSearch/skin/" + fileName;
BartUtils.createXMLHttpRequest(
{
url: resourceUrl,
async: false,
event: "load",
callback: function(event)
{
var httpRequest = event.target;
if(httpRequest.status == 0) // for local file, status is 0 instead of 200
{
BartUtils.registerCss(httpRequest.responseText);
BartUniversalSearch.UniversalSearchClass.initSuccessful = true;
}
else
{
BartUniversalSearch.UniversalSearchClass.initSuccessful = false;
}
}
});
},
openSearchPage: function(url)
{
var type = BartUniversalSearch.Preference.getNewTabType();
switch(type)
{
case 0: // current tab
BartBrowserUtils.openNewLocation(url);
break;
case 1: // new tab forground
BartBrowserUtils.newTab(url, true);
break;
case 2: // new tab background
BartBrowserUtils.newTab(url, false);
break;
}
},
onPageLoad: function(event)
{
var doc = event.originalTarget;
if(doc.nodeName != "#document" || !doc.location)
{
return;
}
if(BartUniversalSearch.UniversalSearchClass.initSuccessful)
{
doc.addEventListener("mouseup", BartUniversalSearch.UniversalSearchClass.onMouseup , false);
}
},
_loadResources: function()
{
//BartUniversalSearch.UniversalSearchClass._loadCssFile("overlay.css");
BartUtils.loadLocalFile("chrome://UniversalSearch/skin/overlay.css",
"text/plain",
function(event)
{
var httpRequest = event.target;
if(httpRequest.status == 0) // for local file, status is 0 instead of 200
{
BartUtils.registerCss(httpRequest.responseText);
BartUniversalSearch.UniversalSearchClass.initSuccessful = true;
}
else
{
BartUniversalSearch.UniversalSearchClass.initSuccessful = false;
}
}
);
BartUniversalSearch.UniversalSearchClass._loadLocalBinaryResource("copy.png", "ICON_COPY");
BartUniversalSearch.UniversalSearchClass._loadLocalBinaryResource("search.png", "ICON_SEARCHDEFAULT");
BartUniversalSearch.UniversalSearchClass._loadLocalBinaryResource("downarrow.png", "ICON_DOWNARROW");
},
_loadLocalBinaryResource: function(fileName, resultName)
{
BartUtils.loadLocalFile("chrome://UniversalSearch/skin/" + fileName,
"text/plain; charset=x-user-defined",
function(event)
{
var httpRequest = event.target;
if(httpRequest.status == 0) // for local file, status is 0 instead of 200
{
BartUniversalSearch.UniversalSearchClass[resultName] = "data:image/png;base64," + BartUtils.Base64.encode(httpRequest.responseText);
}
}
);
},
init: function()
{
BartUniversalSearch.UniversalSearchClass._loadResources();
// init preference and search engines
BartUniversalSearch.Preference.init();
BartUniversalSearch.SearchEngineWrapper.init();
BartUniversalSearch.gSearch = new BartUniversalSearch.UniversalSearchClass();
BartUniversalSearch.gSearch._init();
var appcontent = document.getElementById("appcontent");
if(appcontent)
{
appcontent.addEventListener("DOMContentLoaded", BartUniversalSearch.UniversalSearchClass.onPageLoad, true);
}
},
uninit: function()
{
BartUniversalSearch.SearchEngineWrapper.uninit();
},
onMouseup: function(event)
{
setTimeout(function(){BartUniversalSearch.gSearch.onMouseUp(event);}, 100);
//return BartUniversalSearch.gSearch.onMouseUp(event);
},
initSuccessful: false, // indicates if our tool is init-ed successfully
CMD_ID_COPY: "BartUniversalSearch_ID_Copy",
CMD_ID_SEARCH: "BartUniversalSearch_ID_Search",
CMD_ID_MANAGEENGINE: "BartUniversalSearch_ID_Search_ManageEngines",
CMD_ID_MORESEARCHES: "BartUniversalSearch_ID_MoreSearches",
ATTR_CMD_ID: "cmdid",
ICON_COPY: "",
ICON_SEARCHDEFAULT: "",
ICON_DOWNARROW: ""
}
});
window.addEventListener("load", BartUniversalSearch.UniversalSearchClass.init, false);
window.addEventListener("unload", BartUniversalSearch.UniversalSearchClass.uninit, false);
//window.addEventListener("mouseup", BartUniversalSearch.UniversalSearchClass.onMouseup , false);